#/usr/bin/python
#encoding:utf-8

#App类
import csv
import os

import time

#.activity.InitializeActivity欢迎界面
#.activity.LinkPageActivity引导页
class App(object):
    def __init__(self):
        self.content=""
        self.startTime=0
    #启动app
    def LaunchApp(self):
        cmd='adb shell am start -W -n cn.com.dhc.danlu/cn.com.dhc.danlu.main.activity.InitializeActivity'
        self.content=os.popen(cmd)

    #停止app
    def StopApp(self):
        cmd='adb shell am force-stop cn.com.dhc.danlu'
        os.popen(cmd)

    #获取启动时间
    def GetLaunchedTime(self):
        for line in self.content.readlines():
            if "TotalTime" in line:
                self.startTime=line.split(":")[1]
                break
        return self.startTime

#控制类
class Controller(object):
    def __init__(self,count,pagetype,version):
        self.app=App()
        self.counter=count
        self.pagetype=pagetype
        self.version = version
        self.alldata=[("timestamp","elapsedtime")]

    #单次测试过程
    def testprocess(self):
        self.app.LaunchApp()
        time.sleep(5)
        elapsedtime=self.app.GetLaunchedTime()
        self.app.StopApp()
        time.sleep(3)
        currentTime=self.getCurrentTime()
        self.alldata.append((currentTime,elapsedtime))

    #获取当前的时间戳
    def getCurrentTime(self):
        currentTime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
        return currentTime

    #多次执行测试
    def run(self):
        while self.counter>0:
            self.testprocess()
            self.counter=self.counter-1

    #数据的存储
    def saveDataToCsv(self):
        csvFile=file('launchtime'+self.pagetype+self.version+'.csv','wb')
        writer=csv.writer(csvFile)
        writer.writerows(self.alldata)
        csvFile.close()

class readStartCsv(object):
    def readcsv(self):
        with open('launchtime00.csv', 'rb') as csvfile:
            reader = csv.DictReader(csvfile)
            columnelapsed00 = [row['elapsedtime'] for row in reader]

        with open('launchtime10.csv', 'rb') as csvfile:
            reader = csv.DictReader(csvfile)
            columnelapsed10 = [row['elapsedtime'] for row in reader]

        with open('launchtime20.csv', 'rb') as csvfile:
            reader = csv.DictReader(csvfile)
            columnelapsed20 = [row['elapsedtime'] for row in reader]

        with open('launchtime01.csv', 'rb') as csvfile:
            reader = csv.DictReader(csvfile)
            columnelapsed01 = [row['elapsedtime'] for row in reader]

        with open('launchtime11.csv', 'rb') as csvfile:
            reader = csv.DictReader(csvfile)
            columnelapsed11 = [row['elapsedtime'] for row in reader]

        with open('launchtime21.csv', 'rb') as csvfile:
            reader = csv.DictReader(csvfile)
            columnelapsed21 = [row['elapsedtime'] for row in reader]

        resultCsv={
            'elapsed00':columnelapsed00,
            'elapsed10': columnelapsed10,
            'elapsed20': columnelapsed20,
            'elapsed01': columnelapsed01,
            'elapsed11': columnelapsed11,
            'elapsed21': columnelapsed21
        }
        return resultCsv